package com.hpf.gupao.pattern.singleton.lazy;

/**
 * @description:
 * 优点：写法优雅，利用了java本身的语法特点，性能高，避免了内存浪费
 * 缺点：能够被反射破坏
 * @datetime: 2021/5/18 13:57
 * @author: huangpengfei
 */
public class LazyStaticInnerClassSingleton {

    private LazyStaticInnerClassSingleton(){
        // 防止反射破坏，不够优雅
        if(LazyHolder.INSTANCE != null){
            throw new RuntimeException("Invalid access");
        }
    }

    public static LazyStaticInnerClassSingleton getInstance(){
        return LazyHolder.INSTANCE;
    }

    private static class LazyHolder{
        private static final LazyStaticInnerClassSingleton INSTANCE = new LazyStaticInnerClassSingleton();
    }

}
